package com.fifth.apartment.controller;

import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * TODO
 *
 * @author Dao
 * @since 2024/7/25
 */
@RestController
@Slf4j
@RequestMapping("/app/alipay")
@Api(tags = "支付接口")
public class PayController {

    private final String TRADE_FINISHED = "TRADE_FINISHED";
    private final String TRADE_SUCCESS = "TRADE_SUCCESS";

    @Value("${alipay.app_id}")
    private String notifyUrl;

    @Value("${alipay.app_id}")
    private String returnUrl;

    @Value("${alipay.charset}")
    private String charset;

    @Value("${alipay.alipay_public_key}")
    private String alipayPublicKey;

    @Value("${alipay.sign_type}")
    private String signType;

    @Resource
    private AlipayClient alipayClient;

    /**
     * 下单交易接口
     *
     * @param httpRequest
     * @param httpResponse
     * @throws IOException
     * @deprecated 具体API参数说明参考：https://opendocs.alipay.com/open/02ivbs?scene=21
     */
    @GetMapping("/pay")
    public void pay(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception {

        long outTradeNo = System.currentTimeMillis();
        log.info("支付订单，订单号：" + outTradeNo);

        // 相应请求API的request
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        // 在公共参数中设置回跳和通知地址
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);


        // 封装商品信息
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", outTradeNo);
        bizContent.put("total_amount", 0.01);
        bizContent.put("subject", "测试商品");
        // 沙箱默认值为：QUICK_WAP_WAY
        bizContent.put("product_code", "QUICK_WAP_WAY");
        //bizContent.put("time_expire", "2022-08-01 22:00:00");

        //商品明细信息，按需传入
        //JSONArray goodsDetail = new JSONArray();
        //JSONObject goods1 = new JSONObject();
        //goods1.put("goods_id", "goodsNo1");
        //goods1.put("goods_name", "子商品1");
        //goods1.put("quantity", 1);
        //goods1.put("price", 0.01);
        //goodsDetail.add(goods1);
        //bizContent.put("goods_detail", goodsDetail);

        //扩展信息，按需传入
        //JSONObject extendParams = new JSONObject();
        //extendParams.put("sys_service_provider_id", "2088511833207846");
        //bizContent.put("extend_params", extendParams);

        alipayRequest.setBizContent(bizContent.toString());

        //调用SDK生成表单
        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + charset);
        //直接将完整的表单html输出到页面
        httpResponse.getWriter().write(form);
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}

